﻿using System;
using System.Configuration;
using System.IO;

namespace RemoteDesktop
{
    class PLogs
    {
        /// <summary>
        /// 普通日志
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="info">日志记录</param>
        public static void Info(string className, string info)
        {
            WriteLog("INFO", className, info);
        }

        /// <summary>
        /// 警告日志
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="info">日志记录</param>
        public static void Warn(string className, string info)
        {
            WriteLog("WARN", className, info);
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="info">日志记录</param>
        public static void Error(string className, string info)
        {
            WriteLog("ERROE", className, info);
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="infoLevel">日志级别</param>
        /// <param name="info">日志记录</param>
        private static void WriteLog(string className, string infoLevel, string info)
        {
            string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "/logs";
            if (!Directory.Exists(logFilePath))
            {
                Directory.CreateDirectory(logFilePath);
            }
            string logFileName = logFilePath + "/" + "log_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            if (!File.Exists(logFileName))
            {
                File.Create(logFileName).Close();
            }
            string logFormat = string.Format("[ {0} ] {1}  {2}  {3}",
                                           DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                           className, infoLevel, info);
            StreamWriter sw = File.AppendText(logFileName);
            sw.WriteLine(logFormat);
            sw.Flush();
            sw.Close();
        }

        internal static void Error(object p, object message)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 读取客户设置
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public static string GetSettingString(string settingName)
        {
            try
            {
                if (ConfigurationManager.AppSettings[settingName] != null)
                {
                    string settingString = ConfigurationManager.AppSettings[settingName]
                                                               .ToString();
                    return settingString;
                }
            }
            catch (Exception)
            {
                return "";
            }
            return "";
        }

        /// <summary>
        /// 更新设置
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="valueName"></param>
        public static void UpdateSettingString(string settingName, string valueName)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (ConfigurationManager.AppSettings[settingName] != null)
            {
                config.AppSettings.Settings.Remove(settingName);
            }
            config.AppSettings.Settings.Add(settingName, valueName);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
